package io.framework.modules.water.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.common.utils.DateUtils;
import io.framework.modules.air.entity.AirCarAutoDataEntity;
import io.framework.modules.water.dao.*;
import io.framework.modules.water.dto.WaterDTO;
import io.framework.modules.water.dto.WaterMonitorDTO;
import io.framework.modules.water.entity.WaterAutoDataEntity;
import io.framework.modules.water.entity.WaterEntity;
import io.framework.modules.water.entity.WaterFractureSurfaceEntity;
import io.framework.modules.water.entity.WaterMonitorEntity;
import io.framework.modules.water.service.WaterAutoDataService;
import io.framework.modules.water.service.WaterService;
import io.framework.modules.water.vo.WaterVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 饮用水源地/检测断面 水质类别表
 *
 * @author Gujq email
 * @since 1.0.0 2020-05-22
 */
@Service
public class WaterAutoDataServiceImpl extends CrudServiceImpl<WaterAutoDataDao, WaterAutoDataEntity, WaterAutoDataEntity> implements WaterAutoDataService {

    @Autowired
    private WaterAutoDataDao waterAutoDataDao;


    @Override
    public QueryWrapper<WaterAutoDataEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    @Override
    public PageData<WaterAutoDataEntity>  getWaterAutoListByParams(Map<String, Object> params) {


        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));;
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        Page<WaterAutoDataEntity> p = new Page<>(page, limit);
        IPage<WaterAutoDataEntity> ip = baseDao.getWaterAutoListByParams(p,params);
        List<WaterAutoDataEntity> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), WaterAutoDataEntity.class);

        return new PageData<WaterAutoDataEntity>(dtoList, ip.getTotal());
    }

    @Override
    public Map<String, Object> getwaterAutoDataRateInfo(Map<String, Object> params) {
        Map<String, Object> resultMap = new HashMap<>(3);
        if(params.get("date")!=null) {
            String date = params.get("date").toString();

            params.put("year", date.split("-")[0]);

            String month = date.split("-")[1];
            if (month.substring(0, 1).equals("0")) {
                month = month.replace("0", "");
            }

            params.put("month", month);
            resultMap.put("createDate", params.get("date").toString());
            resultMap.put("waterRateNum", waterAutoDataDao.getWaterAutoDataRateInfo(params));
        }
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> getWaterAutoDataMonthChange(Map<String, Object> params) {
        if (params.get("startDate")==null) {
            Integer dateType = Integer.parseInt(params.get("dateType").toString());
            String date = params.get("date").toString();
            String endDate = "", startDate = "";
            int day = 0;
            if (dateType == 1) {
                //年
                date = DateUtils.format(DateUtils.parse(date, "yyyy"), "yyyy");
                startDate = date + "-01-01";
                endDate = date + "-12-31";
            } else if (dateType == 2) {
                int yyyy = Integer.parseInt(date.substring(0, date.indexOf("-")));
                int q = Integer.parseInt(date.substring(date.indexOf("Q") + 1, date.length()));
                if (q == 1) {
                    startDate = yyyy + "-01-01";
                    endDate = yyyy + "-03-31";
                } else if (q == 2) {
                    startDate = yyyy + "-04-01";
                    endDate = yyyy + "-06-31";
                } else if (q == 3) {
                    startDate = yyyy + "-07-01";
                    endDate = yyyy + "-09-31";
                } else if (q == 4) {
                    startDate = yyyy + "-10-01";
                    endDate = yyyy + "-12-31";
                }
            } else if (dateType == 3) {
                date = date + "-01";
                //一个月有多少天
                day = DateUtils.getDaysOfMonth(DateUtils.parse(date, DateUtils.DATE_MONTH_PATTERN), Calendar.DAY_OF_MONTH);
                startDate = DateUtils.getMonthBegin(date);
                endDate = DateUtils.getMonthEnd(date);
            }
            params.put("startDate", startDate);
            params.put("endDate", endDate);
        }
        List<Map<String, Object>> resultMapList = new ArrayList<>();
        String stationName[] = params.get("stationName").toString().replaceAll(" ", "").split(",");
        for (String name : stationName) {
            Map<String, Object> map = new HashMap<>();
            params.put("stationName", name);
            List<Map<String, Object>> list = baseDao.getWaterAutoDataMonthChange(params);

            map.put("stationName", name);
            map.put("listData", list);
            resultMapList.add(map);
        }
        return resultMapList;
    }

    @Override
    public List<WaterAutoDataEntity> getWaterAutoListNewData(Map<String, Object> params) {
        return baseDao.getWaterAutoListNewData(params);
    }



//    JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
//    String orderField = jsonObject.getString("orderField");
//    String order = jsonObject.getString("order");
//    String time = jsonObject.getString("time");
//    Integer type = jsonObject.getInteger("type");
//    Integer page = jsonObject.getInteger("page");
//    Integer limit = jsonObject.getInteger("limit");
//    QueryWrapper<WaterMonitorEntity> queryWrapper = new QueryWrapper<>();
//        if (StringUtils.isNotBlank(time)) {
//        queryWrapper.eq("date_format(month,'%Y-%m')", time);
//    }
//        if (type != null) {
//        queryWrapper.eq("type", type);
//    }
//    Page<WaterMonitorEntity> p = new Page<>(page, limit);
//        if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) {
//        if (order.equals("desc")) {
//            queryWrapper.orderByDesc(orderField);
//        } else {
//            queryWrapper.orderByAsc(orderField);
//        }
//    }
//    IPage<WaterMonitorEntity> ip = baseDao.selectPage(p, queryWrapper);
//    List<WaterMonitorDTO> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), WaterMonitorDTO.class);
//        return new PageData<WaterMonitorDTO>(dtoList, ip.getTotal());
}
